---
title: "Activism and Control Study 2 Analyses"
author: "Brendan Gallagher"
date: "7/1/2021"
output: html_document
---

Read in and identify data set.
```{r}
Study2 <- read.csv("C:/Users/bgall/OneDrive/Desktop/RESEARCH/Protests and Control/Activism MS/PSPB Submission Materials/Activism and Control Study 2 Data.csv", header=TRUE)
head(Study2)
```

Load necessary packages.
```{r}
library(psych)
library(car)
library(QuantPsyc)
library(mlogit)
library(DescTools)
library(irr)
```
Reverse score control increase, average across control items to produce TotCtrl.
```{r}
Study2$increase_R <- recode(Study2$ctrlincrease, '1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1')
head(Study2$increase_R)
Study2$TotCtrl <- rowMeans(cbind(Study2$ctrlchange, Study2$ctrldecrease, Study2$increase_R), na.rm=TRUE)
head(Study2$TotCtrl)
```
Examine internal consistency of control items.
```{r}
ChangeScale <- Study2[,10:12]
alpha(ChangeScale, title="Control Change Scale", check.keys=TRUE)
```

Obtain demographic descriptives.
```{r}
describe(Followups$age)
describe(Followups$politicalviews)
table(Followups$politicalviews)
table(Followups$gender)
table(Followups$race)
table(Followups$education)
```

Obtain descriptives for activism intentions and control change.
```{r}
describe(Followups$FutureAct)
describe(Followups$TotCtrl)
describe(Followups$ctrlchange)
describe(Followups$ctrldecrease)
describe(Followups$ctrlincrease)
```
Dummy code lifetime activism history (0 = no history, 1 = yes history).
```{r}
Study2$Life_dummy <- ifelse(Study2$LifetimeAct > "3",0,1)
head(Study2$Life_dummy)
tail(Study2$Life_dummy)
table(Study2$Life_dummy)
```
Intercorrelate activism intentions, control change, and political views.
```{r}
CORTEST <- cor.test(Study2$FutureAct, Study2$TotCtrl, alternative="two.sided", method="pearson", na.action=na.exclude)
print(CORTEST)
CORTEST2 <- cor.test(Study2$politicalviews, Study2$FutureAct, alternative="two.sided", method="pearson", na.action=na.exclude)
print(CORTEST2)
CORTEST3 <- cor.test(Study2$politicalviews, Study2$TotCtrl, alternative="two.sided", method="pearson", na.action=na.exclude)
print(CORTEST3)
```

## Hypothesis 1: Linear Regressions
Test assumptions. Establish base model to ensure data conform to linear regression.
```{r}
BaseModel <- lm(FutureAct ~ TotCtrl, data=Study2)
summary(BaseModel)
lm.beta(BaseModel)
confint(BaseModel)
plot(BaseModel$fitted.values, rstandard(BaseModel))
```

## Moderators
Mean center age and control change and compute interaction term.
```{r}
Study2$age_cent <- (Study2$age - 36.76)
Study2$CTRL_cent <- (Study2$TotCtrl - 4.79)
Study2$CtrlxAge <- (Study2$age_cent * Study2$CTRL_cent)
```

Test for CTRL x age interaction.
```{r}
Modela <- lm(FutureAct ~ CTRL_cent + age_cent, data=Study2)
summary(Modela)
lm.beta(Modela)
confint(Modela)
Modelb <- lm(FutureAct ~ CTRL_cent + age_cent + CtrlxAge, data=Study2)
summary(Modelb)
lm.beta(Modelb)
confint(Modela)
anova(Modelb, Modela)
```
Political views.
```{r}
Study2$Politics_Cent <- (Study2$politicalviews - 3.1)
Study2$CTRLxPolitics <- (Study2$Politics_Cent * Study2$CTRL_cent)
```

Test for CTRL x Politics interaction.
```{r}
Modelc <- lm(FutureAct ~ CTRL_cent + Politics_Cent, data=Study2)
summary(Modelc)
lm.beta(Modelc)
confint(Modelc)
Modeld <- lm(FutureAct ~ CTRL_cent + Politics_Cent + CTRLxPolitics, data=Study2)
summary(Modeld)
lm.beta(Modeld)
confint(Modeld)
anova(Modeld, Modelc)
summary(Modeld)$r.squared - summary(Modelc)$r.squared

```
Dummy code political ideology based upon the scale midpoint (0 = liberal, 1 = conservatives). Create interaction term.
```{r}
Study2$politics_split <- car::recode(Study2$politicalviews, "1:4 = 'liberal'; 4:7 = 'conservative'")
head(Study2$politics_split)
table(Study2$politics_split)
Study2$politics_dummy <- ifelse(Study2$politics_split == "conservative", 1,0)
head(Study2$politics_dummy)
Study2$CTRLxViews <- (Study2$CTRL_cent * Study2$politics_dummy)
describe(Study2$politics_dummy)
```
Test two category political views as moderator.
```{r}
Modelm <- lm(FutureAct ~ CTRL_cent + politics_dummy, data=Study2)
summary(Modelm)
lm.beta(Modelm)
confint(Modelm)
Modeln <- lm(FutureAct ~ CTRL_cent + politics_dummy + CTRLxViews, data=Study2)
summary(Modeln)
lm.beta(Modeln)
confint(Modeln)
anova(Modelm,Modeln)
summary(Modeln)$r.squared - summary(Modelm)$r.squared
sd(Study2$CTRL_cent)
describe(Study2$CTRL_cent)
vcov(Modeln)
describe(Study2$politics_dummy)
```

Dummy code gender (0 = female, 1 = male)
```{r}
Study2$gender_dummy <- ifelse(Study2$ï..gender == "1", 0, 1)
head(Study2$gender_dummy)
Study2$CTRLxGender <- (Study2$CTRL_cent * Study2$gender_dummy)
```

Test for CTRL x gender interaction.
```{r}
Modele <- lm(FutureAct ~ CTRL_cent + gender_dummy, data=Study2)
summary(Modele)
lm.beta(Modele)
confint(Modele)
Modelf <- lm(FutureAct ~ CTRL_cent + gender_dummy + CTRLxGender, data=Study2)
summary(Modelf)
lm.beta(Modelf)
confint(Modelf)
anova(Modelf, Modele)
summary(Modelf)$r.squared - summary(Modele)$r.squared
```

Dummy code education level (0 = at least some college, 1 = high school or less).
```{r}
Study2$Educate <- ifelse(Study2$education == "2", 1,0)
head(Study2$Educate)
tail(Study2$Educate)
table(Study2$Educate)
Study2$CTRLxEd <- (Study2$CTRL_cent * Study2$Educate)
```

Test for CTRL x Education interaction.
```{r}
Modelg <- lm(FutureAct ~ CTRL_cent + Educate, data=Study2)
summary(Modelg)
lm.beta(Modelg)
confint(Modelg)
Modelh <- lm(FutureAct ~ CTRL_cent + Educate + CTRLxEd, data=Study2)
summary(Modelh)
lm.beta(Modelh)
confint(Modelh)
anova(Modelh, Modelg)
summary(Modelh)$r.squared - summary(Modelg)$r.squared
```

Recode race. First, dummy code (0 = white, 1 = other) white_or and black_or (0 = black, 1 = other).
```{r}
Study2$White_or <- ifelse(Study2$race == "1", 0, 1)
head(Study2$White_or)
Study2$Black_or <- ifelse(Study2$race == "2", 0, 1)
head(Study2$Black_or)
Study2$CTRLxWhite <- (Study2$CTRL_cent * Study2$White_or)
Study2$CTRLxBlack <- (Study2$CTRL_cent * Study2$Black_or)
table(Study2$Black_or)
```

Test for race x CTRL interactions (first with white as reference group).
```{r}
Modeli <- lm(FutureAct ~ CTRL_cent + White_or, data=Study2)
summary(Modeli)
lm.beta(Modeli)
confint(Modeli)
Modelj <- lm(FutureAct ~ CTRL_cent + White_or + CTRLxWhite, data=Study2)
summary(Modelj)
lm.beta(Modelj)
confint(Modelj)
anova(Modelj,Modeli)
summary(Modelj)$r.squared - summary(Modeli)$r.squared
```

Then with black set as reference group.
```{r}
Modelk <- lm(FutureAct ~ CTRL_cent + Black_or, data=Study2)
summary(Modelk)
lm.beta(Modelk)
confint(Modelk)
Modell <- lm(FutureAct ~ CTRL_cent + Black_or + CTRLxBlack, data=Study2)
summary(Modell)
lm.beta(Modell)
confint(Modell)
anova(Modell,Modelk)
summary(Modell)$r.squared - summary(Modelk)$r.squared
```

Obtain simple slopes for CTRL x Black_or interaction.
```{r}
sd(Study2$CTRL_cent)
describe(Study2$CTRL_cent)
vcov(Modell)
describe(Study2$Black_or)
describe(Study2$FutureAct)
```

## Multinomial Logistic Regression
Define recent activism categories: 1 = Non-activists (reference group), 2 = Activists, 3 = Prospective Activists.
```{r}
Study2$Out <- ifelse(Study2$RecentAct == "1",1,ifelse(Study2$RecentAct == "4",2,3))
Study2$Out <-as.factor(Study2$Out)
head(Study2$Out)
print(Study2$Out)
table(Study2$Out)
```
Format data for mlogit.
```{r}
MultiShaped <- mlogit.data(Study2, choice="Out", shape="wide")
head(Study2)
print(Study2$Out)
```
Obtain descriptives by group.
```{r}
by(data=Study2, INDICES=Study2$Out, FUN=describe)
```

Predict activism engagement category (non activists as reference group) from control change.
```{r}
Mlr <- mlogit(Out ~ 1, data=MultiShaped, reflevel="1")
Mlr1 <- mlogit(Out ~ 1|TotCtrl, data=MultiShaped, reflevel="1")
summary(Mlr)
summary(Mlr1)
Mlr$coefficients
Mlr1$coefficients
confint(Mlr)
confint(Mlr1)
```


